package com.baizhi.service.impl;

import com.baizhi.entity.CmfzUser;
import com.baizhi.mapper.CmfzUserMapper;
import com.baizhi.service.CmfzUserService;
import com.baizhi.utils.ThreadPollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljh
 * @since 2020-08-18
 */
@Slf4j
@Service
public class CmfzUserServiceImpl extends ServiceImpl<CmfzUserMapper, CmfzUser> implements CmfzUserService {

    @Override
    public List<Map> selectUserSex() {
        log.warn("执行了查询男女比列");
        return baseMapper.selectUserSex();
    }

    @Override
    public List<Map> selectUserCountByDate() {
        log.info("执行了用户注册时间");
        return baseMapper.selectUserCountByDate();
    }

    @Override
    public List<Map> selectUserChina() {
        log.info("执行了用户省份");
        return baseMapper.selectUserChina();
    }

    @Override
    public Map selectAllCount() {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("sex",selectUserSex());
        map.put("week",selectUserCountByDate());
        map.put("province",selectUserChina());
        return map;
    }
    @Override
    public Map selectAllCountThread(){
        //1.创建线程计数器
        CountDownLatch countDownLatch = new CountDownLatch(3);
        ThreadPoolExecutor executor = ThreadPollUtil.executor;
        HashMap<Object, Object> map = new HashMap<>();
        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("sex",selectUserSex());
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("week",selectUserCountByDate());
                countDownLatch.countDown();
            }
        });
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                map.put("week",selectUserCountByDate());
                countDownLatch.countDown();
            }
        };

        //executor.execute(runnable);

        //主线程等待  还可以设置超时时间
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return map;

    }

    @Override
    public PageInfo<CmfzUser> selectUserByPageAndCondition(Integer page, Integer limit, String key) {
        PageHelper.startPage(page,limit);
        List<CmfzUser> cmfzUsers = new ArrayList<>();
        if (!StringUtils.isEmpty(key)){
            QueryWrapper<CmfzUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("user_name",key);
            cmfzUsers = baseMapper.selectList(queryWrapper);
        }else {
            cmfzUsers = baseMapper.selectList(null);
        }
        return new PageInfo<CmfzUser>(cmfzUsers);
    }
}
